package com.dc.exceptions;


import com.dc.common.ResponseResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(Throwable e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("系统出错啦，请稍后再试...", null);
    }


    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(NullPointerException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("空指针异常，请检查请求数据是否正确，请稍后再试...", null);
    }


    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(ArrayIndexOutOfBoundsException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("数组下标越界，请稍后再试...", null);
    }


    @ExceptionHandler(ClassNotFoundException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(ClassNotFoundException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("实体类未找到，请稍后再试...", null);
    }


    @ExceptionHandler(NumberFormatException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(NumberFormatException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("字符串转换为数字异常，请稍后再试...", null);
    }


    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(SQLException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("操作数据库异常，请稍后再试...", null);
    }


    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(HttpMessageNotReadableException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("参数格式错误", null);
    }


    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(ClassCastException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("类型强制转换异常，请稍后再试...", null);
    }


    @ExceptionHandler(SQLNonTransientConnectionException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(SQLNonTransientConnectionException e) {
        logger.error("exceptionHandler", e);
        return ResponseResultVO.failResult("数据源异常，请稍后再试...", null);
    }


    /**
     * 自定义异常全局处理
     *
     * @param e 自定义异常类型
     * @return
     */
    @ExceptionHandler(MyBaseException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(MyBaseException e) {
        logger.error("global exception handler: {}", e.getErrorMessage());
        return ResponseResultVO.failResult(e.getMessage(), null);
    }

    /**
     * 自定义异常全局处理
     *
     * @param e 自定义异常类型
     * @return
     */
    @ExceptionHandler(ErrorException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(ErrorException e) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setStatus(400);
        logger.error("global exception handler: {}", e.getErrorMessage());
        return ResponseResultVO.failResult(e.getMessage(), null);
    }

    @ExceptionHandler(TransactionalBizException.class)
    @ResponseBody
    public ResponseResultVO exceptionHandler(TransactionalBizException e) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setStatus(600);
        logger.error("global exception handler: {}", e.getErrorMessage());
        return ResponseResultVO.failResult(e.getMessage(), null);
    }



}
